home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 871 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.6 KB

  1. From: kanze@gabi-soft.fr (J. Kanze)
  2. Message-ID: <KANZE.96Mar27134634@gabi.gabi-soft.fr>
  3. X-Original-Date: 27 Mar 1996 12:46:34 GMT
  4. Path: in1.uu.net!bounce-back
  5. Date: 27 Mar 96 15:19:37 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: initialization of nonlocal static objects
  9. Organization: GABI Software, Sarl.
  10. References: <4j1tm6$bmu@senator-bedfellow.MIT.EDU> <9603251201.AA24987@lts.sel.alcatel.de>
  11.     <ky3f6w679u.fsf@gator.mit.edu>
  12. In-Reply-To: jgealow@mtl.mit.edu's message of 26 Mar 1996 00:30:05 GMT
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBFAgUBMVlcweEDnX0m9pzZAQGaYwF7BsJlMeG7SBWC3E6VxFkBcW6Uq8pxbjOR
  15.     kk3llavPj14JBa6X3l8FUghO6c8WLloX
  16.     =AMuo
  17.  
  18. In article <ky3f6w679u.fsf@gator.mit.edu> jgealow@mtl.mit.edu (Jeffrey
  19. C. Gealow) writes:
  20.  
  21. |> In article <9603251201.AA24987@lts.sel.alcatel.de> James Kanze 
  22. |> US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de> writes:
  23.  
  24. |>   In article <4j1tm6$bmu@senator-bedfellow.MIT.EDU> jgealow@mtl.mit.edu
  25. |>   (Jeffrey C. Gealow) writes:
  26.  
  27. |>   |>   The technique described in the ARM 3.4 annotation cannot be applied
  28. |>   |>   to const objects since the values of the objects may not be changed.
  29.  
  30. |>   No.  Const has nothing to do with the question, since objects only
  31. |>   become const after construction.
  32.  
  33. |> I don't understand James Kanze's comment.  Consider:
  34.  
  35. |>   // file nifty_library.h:
  36. |>   //
  37.  
  38. |>   class X {
  39. |>   public:
  40. |>     int i;
  41. |>     X() : i(10) {};
  42. |>   };
  43.  
  44. |>   extern const X obj;
  45.  
  46. |>   class nifty_counter {
  47. |>     static nifty_count;
  48. |>   public:
  49. |>     nifty_counter() {
  50. |>       if (nifty_count++ == 0) {
  51. |>         obj.i = 5;
  52. |>       }
  53. |>     }
  54. |>   };
  55.  
  56.  
  57. |>   // file nifty_library.cc:
  58. |>   //
  59.  
  60. |>   #include "nifty_library.h"
  61.  
  62. |>   const X obj;
  63.  
  64. |> The intent of the statement obj.i = 5 in nifty_counter::nifty_counter() 
  65. |> is to "initialize" obj.  But formally, obj is initialized by the 
  66. |> default constructor for class X.
  67.  
  68. But normally, you wouldn't try and do it this way, anyway.  It is rare
  69. that initialization is so simple, and even rarer that it only involves
  70. public members.
  71.  
  72. The usual solution would be to provide two constructors for X, a dummy
  73. which does nothing, and the real one used by the nifty_counter.  Thus:
  74.  
  75.     class X
  76.     {
  77.     public :
  78.         enum DontInit { dontInit } ;
  79.                         X( DontInit )   {}
  80.                         X() : i( 5 )    {}
  81.     private :
  82.         int             i ;
  83.     } ;
  84.  
  85. Objects to be initialized by the nifty_counter would be defined thus:
  86.  
  87.     X               obj( X::dontInit ) ;
  88.  
  89. Nifty counter then becomes:
  90.  
  91.     class NiftyCounter
  92.     {
  93.         static int          cnt ;
  94.     public :
  95.                             NiftyCounter()
  96.         {
  97.             if ( cnt == 0 )
  98.                 new( &obj ) X ;
  99.             cnt ++ ;
  100.         }
  101.     } ;
  102.  
  103. (Note that for NiftyCounter to work, it is absolutely essential that the
  104. definition of the object invoke a no-op constructor, since the object
  105. may in fact already be constructed.)
  106.  
  107. As you can see, this works equally well for const objects.
  108. -- 
  109. James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
  110. GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
  111. Conseils en informatique industrielle --
  112.                             -- Beratung in industrieller Datenverarbeitung
  113. ---
  114. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  115. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  116. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  117. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  118. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  119.